﻿using System.Net;
using System.Net.Http;
using System.Text;

namespace KL.UFrame.Core.Exception
{
    /// <summary>
    /// 主动抛出的异常，会被ApiExceptionFilterAttribute捕获，其Message会直接返回给客户端
    /// By:杨超
    /// QQ:489578430
    /// </summary>
    public class ResultException:System.Exception
    {
        public int Code { get; set; } 

        public ResultException(int code,string message):base(message)
        {
            Code = code;
        }

        public HttpResponseMessage ToRequestMessage(HttpStatusCode status= HttpStatusCode.OK)
        {
            return new HttpResponseMessage(status)
            {
                Content  = new StringContent($"{{\"Code\":{Code},\"Message\":\"{Message}\"}}", Encoding.UTF8, "application/json")
            };
        }
    }
}